
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;

using Swaf.Xml;
using Swaf.Container;

namespace Swaf.Logging
{
    /// <summary>
    /// Summary description for DarLogObserver.
    /// </summary>
    public class DarLogObserver : BaseLogObserver
    {
        IApplication m_app;
        string m_logDarName;
        public DarLogObserver()
            : base()
        {
            init();
        }
        public DarLogObserver(bool logCaller)
            : base(logCaller)
        {
            init();
        }
        private void init()
        {
            m_app = Application.currentApp;
            m_logDarName = m_app.resMgr.resolve(@"$LoggingDar$");

            writeToDar(String.Format("************* Begin Logging Session ***************"));
        }
        public void shutDown(IApplication app)
        {
            writeToDar(String.Format("************* End Logging Session ***************"));
        }
        public void writeEntry(IDictionary info)
        {
            try
            {
                BaseException.suppressLogging(true);

                if (!info.Contains("date"))
                    info.Add("date", currentDateTime());
                if (info["message"] is INameAddressSupport)
                {
                    INameAddressSupport val = (INameAddressSupport)info["message"];
                    NameAddressStack stack = new NameAddressStack(val);
                    info.Remove("message");
                    stack.push(new FlexiMap(info));
                    try
                    {
                        m_app.appData.put("Data." + m_logDarName, stack);
                    }
                    catch (Exception e2)
                    {
                        EventLog.WriteEntry("Swaf.Logging.DarLogObserver", String.Format("Error executing logging dar '{0}'.  Error was: {1}", m_logDarName, e2.ToString()), EventLogEntryType.Error);
                    }

                }
                else
                {
                    FlexiMap map = new FlexiMap(info);
                    m_app.appData.put("Data." + m_logDarName, map);
                }
            }
            finally
            {
                BaseException.suppressLogging(false);
            }
        }
        private string currentDateTime()
        {
            return DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss:fffffff tt");
        }

        private void writeToDar(string message)
        {
            IDictionary info = new Hashtable();
            info.Add("message", message);
            info.Add("date", currentDateTime());
            writeEntry(info);
        }
    }
}

